package UI;

import java.awt.event.*;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.awt.*;
import java.sql.*;


import java.awt.Frame.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 *
 * @author Administrator
 */
public class MainFrame extends javax.swing.JFrame {

    public static Connection conn;
    public static String Username;
     Socket cs;
    ObjectOutputStream out;
    ObjectInputStream in;
   
    /** Creates new form MainFrame */

//    public MainFrame() {
//        try {
//          UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
//           initComponents();
//           setBackground();
//             cs = new Socket("localhost", 1234);//mo socket, goi yc kn den server
//            //mo luong out,in de goi nhan du lieu voi server
//             out = new ObjectOutputStream(new DataOutputStream(cs.getOutputStream()));
//             in = new ObjectInputStream(new DataInputStream(cs.getInputStream()));
//        } catch ( IOException | ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
//            ex.printStackTrace();
//        }
//    }
    public MainFrame(String username,String serverName) {
        try {
            //super(parent, modal);
            Username = username;
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            initComponents();
            setBackground();
              cs = new Socket(serverName, 1234);//mo socket, goi yc kn den server
            //mo luong out,in de goi nhan du lieu voi server
            out = new ObjectOutputStream(new DataOutputStream(cs.getOutputStream()));
            in = new ObjectInputStream(new DataInputStream(cs.getInputStream()));
        } catch ( IOException ex) {
            System.out.println(ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    private void setBackground() {
        try {
            // get a Image to make the background
            ImageIcon imageBack = new ImageIcon(getClass().getResource("/Images/MainBackground2.jpg"));
            //Set new image by scale with old image
            Image imageicon = imageBack.getImage();
            Image newimage = imageicon.getScaledInstance(imageicon.getWidth(null) * 6 / 6, imageicon.getHeight(null) * 6 / 6, Image.SCALE_SMOOTH);
            imageBack.setImage(newimage);
            //Set Size Frame from image size
            //  setSize(imageBack.getIconWidth(),imageBack.getIconHeight());
            //Set background Image
            JLabel background = new JLabel(imageBack);
            background.setBounds(0, 0, imageBack.getIconWidth(), imageBack.getIconHeight());
            getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));
            pnMain2.setOpaque(false);
            setContentPane(pnMain2);
        } catch (Exception e) {
        }
        //set locatoin for Frame
        this.setLocationRelativeTo(null);

    }

  

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        pnMain2 = new javax.swing.JPanel();
        btnReader = new javax.swing.JButton();
        btnBook = new javax.swing.JButton();
        btnMyAccount = new javax.swing.JButton();
        btnAboutUs = new javax.swing.JButton();
        btnIssueNote = new javax.swing.JButton();

        jButton1.setText("jButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Library Management System");
        setResizable(false);

        pnMain2.setBackground(new java.awt.Color(0, 215, 255));
        pnMain2.setPreferredSize(new java.awt.Dimension(800, 600));

        btnReader.setBackground(new java.awt.Color(0, 215, 255));
        btnReader.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/id_card.png"))); // NOI18N
        btnReader.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReaderActionPerformed(evt);
            }
        });

        btnBook.setBackground(new java.awt.Color(0, 215, 255));
        btnBook.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Books.png"))); // NOI18N
        btnBook.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBookActionPerformed(evt);
            }
        });

        btnMyAccount.setBackground(new java.awt.Color(0, 215, 255));
        btnMyAccount.setForeground(new java.awt.Color(0, 153, 153));
        btnMyAccount.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/MyAccount.png"))); // NOI18N
        btnMyAccount.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMyAccountActionPerformed(evt);
            }
        });

        btnAboutUs.setBackground(new java.awt.Color(0, 215, 255));
        btnAboutUs.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/AboutUs.png"))); // NOI18N
        btnAboutUs.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btnAboutUs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAboutUsActionPerformed(evt);
            }
        });

        btnIssueNote.setBackground(new java.awt.Color(0, 215, 255));
        btnIssueNote.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/IssueNote.png"))); // NOI18N
        btnIssueNote.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIssueNoteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnMain2Layout = new javax.swing.GroupLayout(pnMain2);
        pnMain2.setLayout(pnMain2Layout);
        pnMain2Layout.setHorizontalGroup(
            pnMain2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnMain2Layout.createSequentialGroup()
                .addGroup(pnMain2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnMain2Layout.createSequentialGroup()
                        .addGap(55, 55, 55)
                        .addComponent(btnReader, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnMain2Layout.createSequentialGroup()
                        .addGap(104, 104, 104)
                        .addComponent(btnBook, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(pnMain2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnMain2Layout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addComponent(btnMyAccount, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(77, 77, 77)
                        .addComponent(btnAboutUs, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnMain2Layout.createSequentialGroup()
                        .addGap(147, 147, 147)
                        .addComponent(btnIssueNote, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(117, 117, 117))
        );
        pnMain2Layout.setVerticalGroup(
            pnMain2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnMain2Layout.createSequentialGroup()
                .addGap(228, 228, 228)
                .addGroup(pnMain2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnMyAccount, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnAboutUs, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnReader, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(74, 74, 74)
                .addGroup(pnMain2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnIssueNote, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
                    .addComponent(btnBook, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(28, 28, 28))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnMain2, javax.swing.GroupLayout.DEFAULT_SIZE, 791, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnMain2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnReaderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReaderActionPerformed
        ReaderClient Reader = new ReaderClient(null, true,out,in);
        Reader.setLocationRelativeTo(this);
        Reader.setVisible(true);
    }//GEN-LAST:event_btnReaderActionPerformed

    private void btnMyAccountActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMyAccountActionPerformed
        MyAccount myaccount = new MyAccount(null, true,out,in);
        myaccount.setLocationRelativeTo(null);
        myaccount.setVisible(true);

    }//GEN-LAST:event_btnMyAccountActionPerformed

    private void btnAboutUsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAboutUsActionPerformed
        AboutDialog about = new AboutDialog(this, true);//
        about.setLocationRelativeTo(this);
        about.setVisible(true);
    }//GEN-LAST:event_btnAboutUsActionPerformed

    private void btnBookActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBookActionPerformed
        frmBook book = new frmBook(null, true,out,in);
        book.setLocationRelativeTo(this);
        book.setVisible(true);

    }//GEN-LAST:event_btnBookActionPerformed

    private void btnIssueNoteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIssueNoteActionPerformed
        // TODO add your handling code here:
        frmIssueNote issueNote = new frmIssueNote(null, true,out,in);
        issueNote.setLocationRelativeTo(this);
        issueNote.setVisible(true);
    }//GEN-LAST:event_btnIssueNoteActionPerformed
      
   
    //private static AboutUs aboutus = new AboutUs(null, true);
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAboutUs;
    private javax.swing.JButton btnBook;
    private javax.swing.JButton btnIssueNote;
    private javax.swing.JButton btnMyAccount;
    private javax.swing.JButton btnReader;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel pnMain2;
    // End of variables declaration//GEN-END:variables
}
